import { app } from 'electron';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BullModule } from '@nestjs/bull';
import {
  // ConfigurationKeyPaths,
  getConfiguration,
} from './../../config/configuration';
// import { AdminModule } from './modules/admin/admin.module';
// import { SharedModule } from './shared/shared.module';
// import { MissionModule } from './mission/mission.module';
import { WSModule } from './modules/ws/ws.module';
import { LoggerModule } from './../shared/logger/logger.module';
import { LoggerModuleOptions, WinstonLogLevel } from './../shared/logger/logger.interface';
import { TypeORMLoggerService } from './../shared/logger/typeorm-logger.service';
import { LOGGER_MODULE_OPTIONS } from './../shared/logger/logger.constants';
import { SharedModule } from './../shared/shared.module';
import { AdminModule } from './modules/admin/admin.module';
import { HttpOrderModule } from './httpOrder/httpOrder.module';
import { MulterModule } from '@nestjs/platform-express';
import { diskStorage } from 'multer';
import { app as electronApp } from 'electron';
import { join } from 'path';
import * as path from 'path';
// import { ServeStaticModule } from '@nestjs/serve-static';
@Module({
  imports: [
    MulterModule.register({
      storage: diskStorage({
        // 指定文件存储目录
        destination: join(electronApp.getPath('userData'), '/UploadFile'),
        // 通过时间戳来重命名上传的文件名
        filename: (_, file, callback) => {
          const fileName = `${new Date().getTime() + path.extname(file.originalname)}`;
          return callback(null, fileName);
        },
      }),
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      load: [getConfiguration],
      envFilePath: [`.env.development`, '.env'],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule, LoggerModule],
      useFactory: (configService: ConfigService, loggerOptions: LoggerModuleOptions) => ({
        autoLoadEntities: true,
        type: 'better-sqlite3',
        database: `${app.getPath('userData')}/Sqlite/mydb.sqlite`,
        entities: [`${__dirname}/**/*.entity{.ts,.js}`],
        synchronize: true,
        // extra: {
        //   timezone: configService.get('database.timezone'), // 时区
        //   useUTC:true
        // },
        timezone: configService.get('database.timezone'), // 时区
        // 自定义日志
        logger: new TypeORMLoggerService(configService.get('database.logging'), loggerOptions),
        extra: {
          foreign_keys: true, // 启用外键约束
        },
      }),
      inject: [ConfigService, LOGGER_MODULE_OPTIONS],
    }),
    BullModule.forRoot({}),
    // custom logger
    LoggerModule.forRootAsync(
      {
        imports: [ConfigModule],
        useFactory: (configService: ConfigService) => {
          return {
            level: configService.get<WinstonLogLevel>('logger.level'),
            consoleLevel: configService.get<WinstonLogLevel>('logger.consoleLevel'),
            timestamp: configService.get<boolean>('logger.timestamp'),
            maxFiles: configService.get<string>('logger.maxFiles'),
            maxFileSize: configService.get<string>('logger.maxFileSize'),
            disableConsoleAtProd: configService.get<boolean>('logger.disableConsoleAtProd'),
            dir: configService.get<string>('logger.dir'),
            errorLogName: configService.get<string>('logger.errorLogName'),
            appLogName: configService.get<string>('logger.appLogName'),
          };
        },
        inject: [ConfigService],
      },
      // global module
      true
    ),
    // custom module
    SharedModule,
    // mission module
    // MissionModule.forRoot(),
    // application modules import
    AdminModule,
    // websocket module
    WSModule,
    HttpOrderModule,
  ],
})
export class AppModule {}
